package com.star.book;


import com.star.book.utils.TimedTasks;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;


/**
 * Quartz配置类
 * */
@Configuration
public class QuartzConfig {

    /**
     * 第一步，创建Job对象
     * */
        @Bean
        public JobDetailFactoryBean Job(){
            JobDetailFactoryBean factory = new JobDetailFactoryBean();
            //关联我们自己的Job类
            factory.setJobClass(TimedTasks.class);

            return factory;
        }
    /**
     * 第二步，创建Cron Trigger
     * */
    @Bean
    public CronTriggerFactoryBean cron(JobDetailFactoryBean jobDetailFactoryBean){
        CronTriggerFactoryBean cron = new CronTriggerFactoryBean();
        cron.setJobDetail(jobDetailFactoryBean.getObject());
        //设置触发时间
        cron.setCronExpression("0 23 23 ? * 1-6");
        return cron;
    }

    /**
     * 第三步，创建Scheduler对象
     * */
    @Bean
    public SchedulerFactoryBean schedule(CronTriggerFactoryBean cron){
        SchedulerFactoryBean schedule = new SchedulerFactoryBean();
        schedule.setTriggers(cron.getObject());
        return schedule;
    }
}